package com.hao.windowloading.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.hao.windowloading.loading.LoadingUtils;

/**
 * Created by hao on 16/12/7.
 */
public abstract class BaseActivity extends AppCompatActivity {
    public LoadingUtils loadingUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        findView();
        loadingUtils = new LoadingUtils(this, dip2px(this, 45) + getBarHeight(this));
        initView();
        setClick();
    }

    public abstract void findView();

    public abstract void initView();

    public abstract void setClick();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        loadingUtils.onDestroy();
        loadingUtils = null;
    }

    /**
     * 转换
     *
     * @param context
     * @param dpValue
     * @return
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public int getBarHeight(Context context) {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        if (result == 0) {
            try {
                Class clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height")
                        .get(object).toString());
                result = context.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
